
let app = getApp();

var util = require('../../utils/util')
import { request } from '../../request/request.js'

Page({
  /**
   * 页面的初始数据
   */
  data: {
    comment_index: 0,
    src: '',
    inputValue: "",
    videoList: {},
    iconList: {},
    commentList: {},
    texticonList:{},
    friends:{},
    privatestate:{},
    show: false,
    url: "",
    vid:'',
    currentPage:1,
    number:0,
    transmit:false
    
  },
  open: async function (e) {  
      let vid=e.currentTarget.id
        this.setData({
          show: true,
          vid:vid
        })
      let result = await request({ url:app.data.url+"home/textprop/"+vid });
        this.setData({
          commentList: result.data.message
        });
  },
  // 评论
  bindInputBlur: async function (e) {
    this.inputValue = e.detail.value
      if (this.inputValue == '') {
          wx.showToast({
            title: '内容不能为空'
          })
        } else {
            this.setData({
              inputValue: '',
            })    
            wx.getStorage({
              key:'userState',
              success:async (res)=>{
                let result = await request({url:app.data.url+'users/getUser',data:{code:res.data}})
                res=await request({
                  url:app.data.url+'home/Addtextprop',
                  data:{
                    img:result.data.message[0].avatarUrl,
                    comment:this.inputValue,
                    date:util.formatTime(new Date),
                    video_id:this.data.vid
                  },
                  method:'post',
                })                
                  let resList= await request({ url:app.data.url+"home/textprop/"+this.data.vid });
                  this.setData({
                    commentList: resList.data.message
                  });         
              }
            })
        }
  },
  handleplay(e) {
    let id = e.currentTarget.id;
    this.id !== id && this.videoContext && this.videoContext.stop();
    this.id = id;
    this.videoContext=wx.createVideoContext(id)
    // console.log("播放");
    wx.hideTabBar({animation: true})
  },
  pause(e){
    // console.log("暂停");
    wx.showTabBar({animation: true})
  },
  videoErrorCallback: function (e) {
    console.log('视频错误信息:')
    console.log(e.detail.errMsg)
  },
  async getVideo() {
    let result = await request({ url: app.data.url + "index/findSomeVideos?currentPage=1&limit=3" });
    this.setData({
      videoList: result.data.message
    });
  },
  async getIcon() {

    let result = await request({ url: app.data.url + "classify/icon" });
    this.setData({
      iconList: result.data.message
    });
  },
  async getComment() {
    let result = await request({ url: app.data.url + "home/textprop" });

    this.setData({
      commentList: result.data.message
    });
  },
  onicon: async function (e) {
    this.data.click == 0 ? 
    this.setData({ click: 1  }) : 
    this.setData({ click: 0 })
    wx.getStorage({
      key:'userState',
      success:async (res)=>{
       let result=  await request({url:app.data.url+'users/getUser',data:{code:res.data}})
       res = await request(
            {
              url:app.data.url+'home/updatevideo',
              data:{
                id:e.currentTarget.id,
                h_image:e.currentTarget.dataset.id,
                user_id:result.data.message[0].id,
                click:e.currentTarget.dataset.click
              },
              method:'put'
            }).then( async resp=>{
              let result= await request({ url:app.data.url+"index/findSomeVideos?currentPage=1&limit=3" });
               this.setData({
                videoList: result.data.message
               });
          }) 
      }
    })
  },
  async clickImg(e) {  
      this.data.click === 0 ? 
      this.setData({ click: 1  }) : 
      this.setData({ click: 0 })
      await request({
          url:app.data.url+'home/updatenumber',
          data:{
            id:e.currentTarget.dataset.id,  
            click:e.currentTarget.dataset.click
          },
          method:'put'
        }).then( async resp=>{
          let resList= await request({ url:app.data.url+"home/textprop/"+this.data.vid });
           this.setData({
             commentList: resList.data.message
           });
        })   
  },
  async getVideo() {
    let result = await request({ url: app.data.url + "index/findSomeVideos?currentPage="+this.data.currentPage+"&limit=3" })
     let videoList = this.data.videoList;
          if(videoList.length>0){       
            videoList.push(...result.data.message)
            this.setData({
              videoList
            });
          }else{
            this.setData({
              videoList: result.data.message
            });
          }
  },
  async getIcon() {
    let result = await request({ url: app.data.url + "classify/icon" });
    this.setData({
      iconList: result.data.message
    });
  },
  async getTextIcon() {
    let result = await request({ url: app.data.url + "home/texticon" });
    this.setData({
      texticonList: result.data.message
    });
  },
  change(e){ 
    if(e.detail.current > this.data.videoList.length-2){
        let currentPage=this.data.currentPage
        this.setData({
           currentPage:++currentPage
        })  
        this.getVideo()
    }
  },
 async onTransmit(e){
    let result = await request({ url: app.data.url + "home/privatestate" });
    this.setData({
      privatestate: result.data.message
    });
    wx.getStorage({
      key:'userState',
      success:async (res)=>{
        let result = await request({ url: app.data.url + "users/findAllFriends",data:{code:res.data} });
        this.setData({
          friends:result.data.message
        })
      }
    })
      this.setData({
        transmit:true,
        vid:e.currentTarget.id
    })
  

  },
  // 分享
  fenxiang: async function(e) {
    // uid, oid, message,chose
    wx.getStorage({
      key:'userState',
      success:async (res)=>{
       let result=  await request({url:app.data.url+'users/getUser',data:{code:res.data}})
     
      //  获取视频id
      let url =  await request(
        {
          url:app.data.url+'home/getVideoUrl',
          data:{
            id:this.data.vid
          },
          method:'get'
        })
        url = url.data.message[0].url;
       await request(
        {
          url:app.data.url+'home/send',
          data:{
            uid:result.data.message[0].id, 
            oid:e.currentTarget.dataset.oid, 
            message:url,
            chose:1
          },
          method:'post'
        })
        wx.showToast({
          title: '分享成功'
        })
      }
    })
    
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    
    this.setData({ url: app.data.url });
    this.getVideo()
    this.getIcon()
    this.getTextIcon()
    if(options.id){
      let video =  await request(
        {
          url:app.data.url+'index/findSomeVideo',
          data:{
            id:options.id
          },
          method:'get'
        })
       let videos=this.data.videoList
       videos[0]=video.data.message[0]
       this.setData({videoList:videos})
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.videoContext = wx.createVideoContext('myVideo')

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.setTheme(app.data.themeid)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})